﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.工厂.车间.报工
{
    public partial class Form罩退投入产出表 : UIPage
    {
        int 开始行;
        int 结束行;
        string 工厂编号 = "3001";
        string FormWorkType = "罩退";
        public Form罩退投入产出表()
        {
            InitializeComponent();
        }

        private void Form罩退投入产出表_Load(object sender, EventArgs e)
        {
            this.uiDatePicker开始日期.Value = DateTime.Today.AddDays(-1);
            this.uiDatePicker结束日期.Value = DateTime.Today.AddDays(-1);
            //pub.ListRank(pub.全局工厂编号, this.uiComboBox查询等级);
            pub.ListRankAll(pub.全局工厂编号, this.uiComboBox查询等级);
            pub.ListWorkLineAll(pub.全局工厂编号, FormWorkType, this.uiComboBox查询产线);
            pub.ListWorkTeamAll(pub.全局工厂编号, "2001-01-G01", this.uiComboBox查询班组);
        }

        private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
        }

        private void uiDataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            decimal 总投入重量 = 0;
            decimal 总产出重量 = 0;

            结束行 = e.RowIndex;
            if (开始行 >= 0 && 结束行 > 0)
            {
                if (开始行 > 结束行)
                {
                    int k = 开始行;
                    开始行 = 结束行;
                    结束行 = k;
                }
                for (int i = 开始行; i <= 结束行; i++)
                {
                    总投入重量 += pub.Todecimal(this.uiDataGridView1.Rows[i].Cells["C投入重量"].Value.ToString());
                    总产出重量 += pub.Todecimal(this.uiDataGridView1.Rows[i].Cells["C产出重量"].Value.ToString());
                }
                if (总投入重量 != 0)
                {
                    this.uiTextBox产出率.Text = Math.Round((pub.Todecimal(总产出重量 / 总投入重量) * 100), 2).ToString();
                }
                else
                {
                    this.uiTextBox产出率.Text = "";
                }
                this.uiTextBox投入重量合计.Text = 总投入重量.ToString();
                this.uiTextBox产出重量合计.Text = 总产出重量.ToString();

            }
        }

        private void uiButton产出查询_Click(object sender, EventArgs e)
        {
            DataTable tb = ListOutPutData();
            uiDataGridView1.AutoGenerateColumns = false;
            this.uiDataGridView1.DataSource = tb;
            this.uiDataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.uiDataGridView1.Refresh();
            for (int i = 0; i < this.uiDataGridView1.Rows.Count; i++)
            {
                if (this.uiDataGridView1.Rows[i].Cells["C产出等级"].Value.ToString() == "B")
                {
                    this.uiDataGridView1.Rows[i].Cells["C产出等级"].Style.BackColor = Color.FromArgb(255, 255, 128);
                }
                else if (this.uiDataGridView1.Rows[i].Cells["C产出等级"].Value.ToString() == "S")
                {
                    this.uiDataGridView1.Rows[i].Cells["C产出等级"].Style.BackColor = Color.FromArgb(255, 0, 0);
                }
            }
        }

        private DataTable ListOutPutData()
        {
            string sql;
            sql = "SELECT   row_number() over(order by 投入卷号) 序号,[投入卷号], [投入规格], [投入重量], [投入材质], [生产日期], [产出卷号], [产出规格], [产出材质], [产出重量], [产出等级]," +
                " [去向], [产线名称], [班组名称], [班别], [CreateDate], [客户], [包装], [长度], [产地], [品名] FROM      View_罩退投入产出表 ";
            sql += " where 1=1  and 生产日期 >='" + this.uiDatePicker开始日期.Value.ToString() + "'";
            sql += " and 生产日期 < '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            string sql2 = "select isnull(sum(投入重量),0) ,isnull(sum(产出重量),0),isnull(sum(长度),0)  FROM      View_罩退投入产出表 ";
            sql2 += " where 1=1  and 生产日期 >='" + this.uiDatePicker开始日期.Value.ToString() + "'";
            sql2 += " and 生产日期 < '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (this.uiComboBox查询产线.SelectedText != "全部")
            {
                sql += " and 产线名称='" + this.uiComboBox查询产线.SelectedText + "' ";
                sql2 += " and 产线名称='" + this.uiComboBox查询产线.SelectedText + "' ";
            }
            if (this.uiComboBox查询班组.SelectedText != "全部")
            {
                sql += " and 班组名称='" + this.uiComboBox查询班组.SelectedText + "' ";
                sql2 += " and 班组名称='" + this.uiComboBox查询班组.SelectedText + "' ";
            }
            if (this.uiTextBox投入卷号包含.Text != "")
            {
                sql += " and 投入卷号 like '%" + this.uiTextBox投入卷号包含.Text.Trim().Replace("--", "") + "%' ";
                sql2 += " and 投入卷号 like '%" + this.uiTextBox投入卷号包含.Text.Trim().Replace("--", "") + "%' ";
            }
            if (this.uiTextBox产出卷号包含.Text != "")
            {
                sql += " and 产出卷号 like '%" + this.uiTextBox产出卷号包含.Text.Trim().Replace("--", "") + "%' ";
                sql2 += " and 产出卷号 like '%" + this.uiTextBox产出卷号包含.Text.Trim().Replace("--", "") + "%' ";
            }
            if (uiComboBox查询等级.SelectedText == "全部")
            {

            }
            else
            {
                sql += " and 产出等级 = '" + uiComboBox查询等级.SelectedText + "'";
                sql2 += " and 产出等级 = '" + uiComboBox查询等级.SelectedText + "'";
            }

            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql2);

            if (tb2 != null && tb2.Rows.Count > 0)
            {
                this.uiTextBox投入重量合计.Text = tb2.Rows[0][0].ToString();
                this.uiTextBox产出重量合计.Text = pub.Todecimal(tb2.Rows[0][1].ToString(), 3);
                this.uiTextBox产出米数合计.Text = tb2.Rows[0][2].ToString();
                decimal 总投入重量 = pub.Todecimal(this.uiTextBox投入重量合计.Text);
                decimal 总产出重量 = pub.Todecimal(this.uiTextBox产出重量合计.Text);

                if (总投入重量 > 0)
                {
                    this.uiTextBox产出率.Text = Math.Round((pub.Todecimal(总产出重量 / 总投入重量) * 100), 2).ToString();
                }
                else
                {
                    this.uiTextBox产出率.Text = "";
                }
            }
            this.uiTextBox卷数.Text = tb.Rows.Count.ToString();

            return tb;
        }
    }
}
